package edu.hit.crawler.util;

import edu.hit.memcached.tool.MemCachedClient;
import edu.hit.memcached.tool.SockIOPool;

/*
 * memcached util class
 * 使用前启动memcached服务
 * 启动命令：memcached -d -m 100 -p 11211 -c 128 -P ~/memcached.pid
 */

public class MemcachedUtil {

	private static String[] servers = { "localhost:11211" };
	private static MemCachedClient mcc = new MemCachedClient();

	static {
		SockIOPool pool = SockIOPool.getInstance();
		pool.setServers(servers);

		// 设置初始连接数、最小和最大连接数以及最大处理时间
		pool.setInitConn(5);
		pool.setMinConn(5);
		pool.setMaxConn(250);
		pool.setMaxIdle(1000 * 60 * 60 * 6);

		// 设置主线程的睡眠时间
		pool.setMaintSleep(30);

		// 设置TCP的参数，连接超时等
		pool.setNagle(false);
		pool.setSocketConnectTO(3 * 1000); // Socket阻塞建立连接的等待时间
		pool.setSocketTO(3 * 1000); // Socket阻塞读取数据的超时时间

		// 初始化连接池
		pool.initialize();
	}

	public static MemCachedClient getMemcachedClient() {
		return mcc;
	}
}
